//
//  TZHomeCell.m
//  OurFramework
//
//  Created by 949699582 on 2019/8/5.
//  Copyright © 2019 com.cunyin.paywaiter. All rights reserved.
//

#import "TZHomeCell.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
#import "YYKit.h"
#import "TZHomeModel.h"
#import "StuckHelper.h"
static float CELL_HEIGHT = 15.0 + 80 + 15 + 1;
@interface TZHomeCell ()

@property (nonatomic,strong) UIImageView *header; // 用户头像
@property (nonatomic,strong) UILabel *nameLB;
@end

@implementation TZHomeCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"TZHomeCell";
    TZHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[TZHomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.contentView.backgroundColor = [UIColor orangeColor];
        cell.contentView.layer.cornerRadius = 5;
        cell.layer.masksToBounds = YES;
        
        // 适配UI
        
        cell.name.frame = CGRectMake(60, 60, 100, 40);
        cell.header.frame = CGRectMake(0, 0, 0, 0);
        
        [cell.header mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView);
            make.size.mas_equalTo(CGSizeMake(40, 40));
            make.left.equalTo(cell.contentView).with.offset(10);
        }];
        [cell.nameLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView);
            make.left.equalTo(cell.header.mas_right).with.offset(5);
            make.height.mas_equalTo(40);
        }];
        
        
    }
    
    return cell;
}

// 设置单元格在页面上的显示
- (void)setFrame:(CGRect)frame{
    frame.origin.x += 10;
    frame.origin.y += 10;
    frame.size.height -= 10;
    frame.size.width -= 20;
    [super setFrame:frame];
}

+ (CGFloat)getCellHeight{
    return CELL_HEIGHT;
}

// 添加数据
- (void)setModel:(TZHomeModel *)model{
    _model = model;
    
    //赋值
    if (model.name != nil) {
        self.nameLB.text = model.name;
    }
    if (model.header != nil) {
        [[StuckHelper shareHelper] addTaskBlock:^BOOL{
//            [self.header sd_setImageWithURL:[NSURL URLWithString:model.header]];
            [self.header sd_setImageWithURL:[NSURL URLWithString:model.header] placeholderImage:[UIImage imageNamed:@""]];
            return YES;
        } withKey:model.header];
        
    }
}

#pragma mark - 懒加载
- (UILabel *)name{
    if (!_nameLB) {
        
        _nameLB = [[UILabel alloc] init];
        [self.contentView addSubview:_nameLB];
    }
    return _nameLB;
}

- (UIImageView *)header{
    if (!_header) {
        _header = [[UIImageView alloc] init];
        [self.contentView addSubview:_header];
    }
    return _header;
}
@end
